import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import fun.carliels.doraemon.common.crypto.AES;
import okhttp3.*;
import org.apache.commons.lang3.StringUtils;

import java.io.*;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class CxdsheImport {

    public static void main(String[] args) throws Exception {
        String filePath = "D:\\doc\\创新大赛合作\\batch_99.csv";
        String aesKey = "34A7663E810A333C";
        String cookie = "sensorsdata2015jssdkcross=%7B%22distinct_id%22%3A%221816d384c6a4aa-02b98e5cd9293a2-714e2d28-1327104-1816d384c6b69e%22%2C%22first_id%22%3A%22%22%2C%22props%22%3A%7B%22%24latest_traffic_source_type%22%3A%22%E7%9B%B4%E6%8E%A5%E6%B5%81%E9%87%8F%22%2C%22%24latest_search_keyword%22%3A%22%E6%9C%AA%E5%8F%96%E5%88%B0%E5%80%BC_%E7%9B%B4%E6%8E%A5%E6%89%93%E5%BC%80%22%2C%22%24latest_referrer%22%3A%22%22%7D%2C%22identities%22%3A%22eyIkaWRlbnRpdHlfY29va2llX2lkIjoiMTgxNmQzODRjNmE0YWEtMDJiOThlNWNkOTI5M2EyLTcxNGUyZDI4LTEzMjcxMDQtMTgxNmQzODRjNmI2OWUifQ%3D%3D%22%2C%22history_login_id%22%3A%7B%22name%22%3A%22%22%2C%22value%22%3A%22%22%7D%2C%22%24device_id%22%3A%221816d384c6a4aa-02b98e5cd9293a2-714e2d28-1327104-1816d384c6b69e%22%7D; Hm_lvt_35b8d7c43d57a69968585d821994e506=1676377156,1676380791,1676510609,1677584264; Hm_lpvt_35b8d7c43d57a69968585d821994e506=1677584264; SESSION=MGU1ODQ1ZDktODRhZC00NWJjLWI4MmMtMTQ5OTk0ZmJjZjQ1";
        FileReader fileReader = new FileReader(new File(filePath));
        BufferedReader bufferedReader = new BufferedReader(fileReader);
        String line = bufferedReader.readLine();
        StringBuilder sb = new StringBuilder(line).append("\n");
        while ((line = bufferedReader.readLine()) != null) {
            Thread.sleep(500);
            System.out.println(line);
            String[] item = line.split(",");
            String plaintext = String.format("{\"cellPhone\":\"%s\",\"field\":\"manufacture\",\"sceneInfoId\":\"12182976128143494\",\"cooperationType\":\"0\",\"email\":\"%s\",\"name\":\"%s\",\"companyName\":\"%s\",\"coopDemandDesc\":\"%s入驻制证\"}"
                    , item[2], item[3], item[1], item[0], item[0]);
            boolean success = requset(plaintext, aesKey, cookie);
            if (success) {
                sb.append(line).append("\n");
            }
        }
        System.out.println(">>>>>>>> 发送成功结果: ");
        System.out.println(sb);
    }


    public static boolean requset(String param, String aesKey, String cookie) throws Exception {
        if (StringUtils.isBlank(param)) {
            return false;
        }
        param = AES.encrypt(param, aesKey);
        OkHttpClient client = new OkHttpClient().newBuilder()
                .build();
        MediaType mediaType = MediaType.parse("application/json;charset=UTF-8");
        RequestBody body = RequestBody.create(mediaType, param);
        Request request = new Request.Builder()
                .url("https://kxlabs.10086.cn/proxy_webbas/cap/scene/cooperation/add")
                .method("POST", body)
                .addHeader("Accept", "*/*")
                .addHeader("Accept-Language", "zh-CN,zh;q=0.9")
                .addHeader("Cache-Control", "no-cache")
                .addHeader("Connection", "keep-alive")
                .addHeader("Content-Type", "application/json;charset=UTF-8")
                .addHeader("Cookie", cookie)
                .addHeader("Origin", "https://kxlabs.10086.cn")
                .addHeader("Pragma", "no-cache")
                .addHeader("Referer", "https://kxlabs.10086.cn/changjing/cooperation")
                .addHeader("Sec-Fetch-Dest", "empty")
                .addHeader("Sec-Fetch-Mode", "cors")
                .addHeader("Sec-Fetch-Site", "same-origin")
                .addHeader("User-Agent", "Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1")
                .build();
        Response response = client.newCall(request).execute();
        if (response.isSuccessful()) {
            String respBody = AES.decrypt(response.body().string(), aesKey);
            JSONObject resp = JSONObject.parseObject(respBody);
            if ("200".equals(resp.getString("status"))) {
                return true;
            }
            System.out.println(resp);
        } else {
            System.out.println(response.body().string());
        }
        return false;
    }
}
